package com.mybatis.lo.model;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;

/**
 * 查询声明对象，相当于一个select标签
 * @author zhangbh
 * 2023-06-08 08:29
 **/
@Data
public class MappedStatement {

    /**
     * 方案ID
     */
    private String id;
    /**
     * 返回类型
     */
    private Class<?> resultType;
    /**
     * 返回类型字符串
     */
    @Getter(AccessLevel.PRIVATE)
    private String resultTypeStr;
    /**
     * 声明查询类型
     */
    private String statementType;
    /**
     * 查询组装对象，可以获取到JDBC可以执行的sql和对应的参数映射列表
     */
    private SqlSource sqlSource;

    public MappedStatement(String id, String statementType, SqlSource sqlSource, String resultType) {
        this.id = id;
        this.statementType = statementType;
        this.sqlSource = sqlSource;
        this.resultTypeStr = resultType;
        try {
            this.resultType = Class.forName(resultType);
        } catch (ClassNotFoundException e) {
            System.out.println("解析到返回类型错误");
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取到绑定的Sql
     * @param param 入参对象
     * @return 绑定sql
     */
    public BoundSql getBoundSql(Object param) {
        return sqlSource.getBoundSql(param);
    }
}
